package org.gdstash.ui.util;

import org.gdstash.item.GDStash;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.util.GDColor;
import org.gdstash.util.GDConstants;
import org.gdstash.util.GDLog;
import org.gdstash.util.GDMsgFormatter;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GDStashInfoList {
    public static List<GDStashFileInfo> gdStashFileInfos;
    public static List<String> modDirs;
    public static GDStashFileInfo defaultStash;

    public static void findStashes(final GDStashFrame frame, final GDStashFileInfo reloadInfo, final GDLog log) {
        final List<GDStashFileInfo> infos = GDStashInfoList.gdStashFileInfos;
        GDStashInfoList.gdStashFileInfos = new LinkedList<GDStashFileInfo>();
        (GDStashInfoList.modDirs = new LinkedList<String>()).add("");
        if (GDStashFrame.iniConfig == null) {
            return;
        }
        if (GDStashFrame.iniConfig.sectDir.savePath == null) {
            return;
        }
        if (GDStashFrame.iniConfig.sectDir.savePath.isEmpty()) {
            return;
        }
        final File file = new File(GDStashFrame.iniConfig.sectDir.savePath);
        if (file.isDirectory()) {
            processStashDir(file, 0);
            processStashDir(file, 1);
            processModDir(file);
        }
        if (file.isFile()) {
            processStashFile(file);
        }
        adjustStashInfos(infos, reloadInfo, log);
        if (frame != null) {
            if (frame.pnlTransfer != null) {
                frame.pnlTransfer.refreshStashSelection();
            }
            if (frame.pnlCraft != null) {
                frame.pnlCraft.refreshStashSelection();
            }
        }
    }

    private static void processModDir(final File dir) {
        try {
            final File[] files = dir.listFiles();
            if (files == null) {
                return;
            }
            for (int i = 0; i < files.length; ++i) {
                final File file = files[i];
                if (file != null && file.isDirectory()) {
                    String path = file.getCanonicalPath();
                    if (path.length() > GDStashFrame.iniConfig.sectDir.savePath.length()) {
                        path = path.substring(GDStashFrame.iniConfig.sectDir.savePath.length() + 1);
                        if (!path.equals("main") && !path.equals("user") && !GDStashInfoList.modDirs.contains(path)) {
                            GDStashInfoList.modDirs.add(path);
                        }
                    }
                }
            }
        } catch (IOException ex) {
        }
    }

    private static void processStashDir(final File dir, final int recDepth) {
        if (dir == null) {
            return;
        }
        if (recDepth < 0) {
            return;
        }
        final File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (int i = 0; i < files.length; ++i) {
            final File file = files[i];
            if (file != null) {
                if (file.isDirectory()) {
                    processStashDir(file, recDepth - 1);
                }
                if (file.isFile() && recDepth == 0) {
                    processStashFile(file);
                }
            }
        }
    }

    private static void processStashFile(final File file) {
        final String s = file.getName().toUpperCase(GDConstants.LOCALE_US);
        if (s.equals("TRANSFER.GST") || s.equals("TRANSFER.GSH") || s.equals("TRANSFER.BST") || s.equals("TRANSFER.BSH") || s.equals("TRANSFER.CST") || s.equals("TRANSFER.CSH")) {
            final GDStashFileInfo info = new GDStashFileInfo();
            info.stashFile = file;
            GDStashInfoList.gdStashFileInfos.add(info);
        }
    }

    public static void adjustStashInfo(final GDStashFileInfo info) {
        if (info.stashFile == null) {
            return;
        }
        try {
            info.fileName = info.stashFile.getCanonicalPath();
        } catch (IOException ex) {
            info.fileName = null;
        }
        StashExt stashExt = StashExt.GENERIC;
        final String fn = info.stashFile.getName().toUpperCase(GDConstants.LOCALE_US);
        String sHC = null;
        if (fn.equals("TRANSFER.GSH")) {
            sHC = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_STASH_HARDCORE");
        }
        if (fn.equals("TRANSFER.GST")) {
            sHC = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_STASH_SOFTCORE");
        }
        if (fn.equals("TRANSFER.BSH")) {
            sHC = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_STASH_HC_BASE");
            stashExt = StashExt.VANILLA;
        }
        if (fn.equals("TRANSFER.BST")) {
            sHC = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_STASH_SC_BASE");
            stashExt = StashExt.VANILLA;
        }
        if (fn.equals("TRANSFER.CSH")) {
            sHC = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_STASH_HC_AOM");
            stashExt = StashExt.AOM;
        }
        if (fn.equals("TRANSFER.CST")) {
            sHC = GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_STASH_SC_AOM");
            stashExt = StashExt.AOM;
        }
        String dir = null;
        try {
            dir = info.stashFile.getParentFile().getCanonicalPath();
        } catch (IOException ex2) {
            dir = null;
        }
        String s = null;
        if (dir == null) {
            dir = info.stashFile.getParentFile().getName();
            final Object[] sArgs = {sHC, dir};
            s = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_STASH_SELECT_MOD", sArgs);
        } else if (dir.length() <= GDStashFrame.iniConfig.sectDir.savePath.length()) {
            final Object[] sArgs = {sHC};
            s = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_STASH_SELECT_MAIN", sArgs);
        } else {
            dir = info.stashFile.getParentFile().getName();
            final Object[] sArgs = {sHC, dir};
            s = GDMsgFormatter.format(GDMsgFormatter.rbUI, "TXT_STASH_SELECT_MOD", sArgs);
        }
        if (stashExt == StashExt.VANILLA) {
            s = "<html>" + GDColor.HTML_COLOR_CHAR_GD + s + "</font>" + "</html>";
        }
        if (stashExt == StashExt.AOM) {
            s = "<html>" + GDColor.HTML_COLOR_CHAR_AOM + s + "</font>" + "</html>";
        }
        info.stashInfo = s;
    }

    public static void adjustStashInfos(final List<GDStashFileInfo> infos, final GDStashFileInfo reloadInfo, final GDLog log) {
        if (GDStashInfoList.gdStashFileInfos == null) {
            return;
        }
        boolean found = false;
        GDStashFileInfo first = null;
        for (final GDStashFileInfo info : GDStashInfoList.gdStashFileInfos) {
            adjustStashInfo(info);
            if (first == null) {
                first = info;
            }
            if (GDStashFrame.iniConfig.sectHistory.lastStash != null && GDStashFrame.iniConfig.sectHistory.lastStash.equals(info.fileName)) {
                if (info.gdStash == null) {
                    found = true;
                    info.gdStash = new GDStash(info.stashFile, log);
                    if (info.gdStash.hasStashErrors()) {
                        info.gdStash = null;
                    }
                }
                GDStashInfoList.defaultStash = info;
                GDStashFrame.iniConfig.sectHistory.lastStash = info.fileName;
            }
            if (infos != null) {
                for (final GDStashFileInfo info2 : infos) {
                    if (info.fileName.equals(info2.fileName) && info2.gdStash != null && info2 != reloadInfo && info2.gdStash.hasChanged()) {
                        info.gdStash = info2.gdStash;
                    }
                    if (reloadInfo != null && info.fileName.equals(reloadInfo.fileName)) {
                        if (info.gdStash != null) {
                            info.gdStash.read(null);
                            if (info.gdStash.hasStashErrors()) {
                                info.gdStash = null;
                            }
                        } else {
                            info.gdStash = new GDStash(info.stashFile);
                        }
                        GDStashFrame.iniConfig.sectHistory.lastStash = info.fileName;
                    }
                }
            }
        }
        if (!found && (GDStashInfoList.defaultStash = first) != null) {
            GDStashFrame.iniConfig.sectHistory.lastStash = first.fileName;
            if (first.gdStash == null) {
                first.gdStash = new GDStash(first.stashFile);
                if (first.gdStash.hasStashErrors()) {
                    first.gdStash = null;
                }
            }
        }
    }

    public static String[] getStashInfos() {
        int size = 1;
        if (GDStashInfoList.gdStashFileInfos != null) {
            size += GDStashInfoList.gdStashFileInfos.size();
        }
        String[] cis = new String[size];
        cis[0] = "";
        if (GDStashInfoList.gdStashFileInfos == null) {
            return cis;
        }
        if (GDStashInfoList.gdStashFileInfos.size() == 0) {
            return cis;
        }
        --size;
        cis = new String[size];
        int i = 0;
        for (final GDStashFileInfo info : GDStashInfoList.gdStashFileInfos) {
            cis[i] = info.stashInfo;
            ++i;
        }
        return cis;
    }

    private enum StashExt {
        GENERIC,
        VANILLA,
        AOM,
        FG;
    }

    public static class GDStashFileInfo {
        public File stashFile;
        public String fileName;
        public String stashInfo;
        public GDStash gdStash;

        public void setFile(final File file) {
            this.stashFile = file;
            GDStashInfoList.adjustStashInfo(this);
        }
    }
}
